package org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.entity.User;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.mapper.UserMapper;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Ben
 * @Date: 2021/6/19 16:35
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    protected UserMapper userMapper;

    @Override
    public boolean insert(User user) {
        List<User> userList = new LambdaQueryChainWrapper<User>(userMapper)
                .eq(User::getUserName,user.getUserName())
                .eq(User::getPassword,user.getPassword())
                .eq(User::getEmail,user.getEmail())
                .eq(User::getPhone,user.getPhone())
                .list();
        if (userList.size() == 0) {
            return userMapper.insert(user) == 1;
        } else {
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        return userMapper.deleteById(id) == 1;
    }

    @Override
    public boolean update(User user) {
        return userMapper.updateById(user) == 1;
    }

    @Override
    public IPage<User> selectUserByPage(Integer pageNumber, Integer pageSize) {
        Page<User> userPage = new Page<>(pageNumber,pageSize);
        IPage<User> userIPage = userMapper.selectPage(userPage,null);
        return userIPage;
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectList(null);
    }
    @Override
    public User select(Integer id) {
        return userMapper.selectById(id);
    }
}
